package com.kgc.listener;

import com.alibaba.fastjson.JSON;
import com.kgc.bean.GoodsCarinfo;
import com.kgc.bean.ShoppingCart;
import com.kgc.constant.ActiveMqConstant;
import com.kgc.util.RedisUtils;
import com.kgc.util.ResultBuildUtil;
import com.kgc.util.ReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

@Component
@Slf4j
public class ShopCartConsumer {
    @Autowired
    private RedisUtils redisUtils;

    @JmsListener(destination = ActiveMqConstant.QUEUE_NAME_TEST, containerFactory = ActiveMqConstant.QUEUE_NAME_lISTENER_FACTORY)
    @SendTo(ActiveMqConstant.QUEUE_NAME_PRSP_TEST)
    public ReturnResult<Map<String, String>> receiveMessageFromQueue(String message) {
    log.info("=====");
        Map<String, String> ReturnData = new HashMap<>();

        GoodsCarinfo goodsCarinfo = JSON.parseObject(message, GoodsCarinfo.class);
//        log.info("监听到的信息是:{}",goodsCarinfo.toString());
        Object obj = redisUtils.get("cart:" + goodsCarinfo.getUserid());
        ShoppingCart shoppingCart = JSON.parseObject(obj.toString(), ShoppingCart.class);
        List carinfoList = JSON.parseObject(shoppingCart.getCarinfolist(), List.class);
        AtomicReference<Boolean> flag = new AtomicReference<>(true);
        List<GoodsCarinfo> list = new ArrayList<>();
        if (carinfoList != null) {
            carinfoList.forEach(object -> {
                GoodsCarinfo gcinfo = JSON.parseObject(object.toString(), GoodsCarinfo.class);
                log.info(gcinfo.toString());
                if (gcinfo.getGoodsid().equals(goodsCarinfo.getGoodsid()) && gcinfo.getBusinessid().equals(goodsCarinfo.getBusinessid())) {
                    gcinfo.setQuantity(gcinfo.getQuantity() + goodsCarinfo.getQuantity());
                    object = JSON.toJSONString(gcinfo);
                    flag.set(false);
                    ReturnData.put("msg", "该商品已经存在于购物车");
                    log.info("该商品已经存在于购物车，目前已经合并");

                }
                GoodsCarinfo carinfo = JSON.parseObject(object.toString(), GoodsCarinfo.class);
                list.add(carinfo);
            });
            carinfoList = list;
            log.info("该商品已经存在于购物车，目前已经合并carinfoList:{}", carinfoList.toString());
        } else {
            carinfoList = new ArrayList<>();
        }

        if (flag.get()) {
            carinfoList.add(goodsCarinfo);
            ReturnData.put("msg", "已经加入购物车");
            log.info("++++++++++++已经加入购物车++++++++++");
        }

        shoppingCart.setCarinfolist(JSON.toJSONString(carinfoList));
        redisUtils.set("cart:" + goodsCarinfo.getUserid(), shoppingCart);
        log.info("++++++++++++从购物车队列中消费一条消息：{}++++++++++", message);

        log.info("$$$$$$$$$$$$$$$$$$$JSON.toJSONString(shoppingCart):{}$$$$$$$$$$$$$$$$$$$$$$$$$$", JSON.toJSONString(shoppingCart));
        ReturnData.put("cart:" + goodsCarinfo.getUserid(), JSON.toJSONString(shoppingCart));
        return ResultBuildUtil.success(ReturnData);

    }
}